package cn.kgc.fcm.dsbank.controller;


import cn.kgc.fcm.dsbank.dto.LoginDto;
import cn.kgc.fcm.dsbank.dto.ResultJson;
import cn.kgc.fcm.dsbank.entity.Employee;
import cn.kgc.fcm.dsbank.service.EmployeeService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import org.apache.logging.log4j.spi.LoggerContextFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author christopher
 * @since 2022-06-14
 */
@RestController
@RequestMapping("/emp")
@Api(tags = "员工管理模块",value = "EmployeeController")

public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    private static final Logger log = LoggerFactory.getLogger(EmployeeController.class);


    @ApiOperation("显示员工信息")
    @GetMapping("/list")
    @ApiResponses({
            @ApiResponse(responseCode = "2000", description = "保存成功"),
            @ApiResponse(responseCode = "4000", description = "保存失败")
    })
    public ResultJson showEmp(){

        List<Employee> emps = employeeService.list(null);

        if(CollectionUtils.isEmpty(emps)){
            return ResultJson.buildError("没有员工信息");
        }

        return ResultJson.buildSuccess(emps);
    }

    @ApiOperation("根据id查询")
    @GetMapping("/{empId}")
    public ResultJson showEmp(@PathVariable @ApiParam(name = "empId",value = "员工id",example = "1") Integer empId){
        Employee emp = employeeService.getById(empId);

        if(Objects.isNull(emp)){
            return ResultJson.buildError("没有该员工");
        }
        return ResultJson.buildSuccess(emp);
    }

    @ApiOperation("用户登录")
    @PostMapping("/login")
    public ResultJson login(@RequestBody Employee employee){

        log.info("登录数据：{}",employee);

        QueryWrapper<Employee> employeeQueryWrapper = new QueryWrapper<>(employee);

        Employee loginEmp = employeeService.getOne(employeeQueryWrapper);

        if(Objects.isNull(loginEmp)){
            return ResultJson.buildError("用户名密码错");
        }
        return ResultJson.buildSuccess(loginEmp);
    }

    @ApiOperation("用户登录1")
    @PostMapping("/login1")
    public ResultJson login1(@RequestBody LoginDto loginDto){

        log.info("登录信息：{}",loginDto);
        return ResultJson.buildSuccess();
    }

    @ApiOperation("用户登录2")
    @PostMapping("/login2")
    public ResultJson login2(LoginDto loginDto){

        log.info("登录信息：{}",loginDto);
        return ResultJson.buildSuccess();
    }

    @PostMapping("/")
    public ResultJson addEmp(@RequestBody Employee emp){

        log.info("传入的数据：{}",emp);


        boolean save = employeeService.save(emp);
//
//
//
        if (!save){
            return ResultJson.buildError("添加失败");
        }

        return ResultJson.buildSuccess();

    }

    @PatchMapping("/")
    public ResultJson updateEmp(@RequestBody Employee employee){
        boolean flag = employeeService.saveOrUpdate(employee);
//
//        if (!flag){
//            return ResultJson.buildError("修改不成功");
//        }

        return ResultJson.buildSuccess();
    }

    @DeleteMapping("/{empId}")
    public ResultJson delEmp(@PathVariable Integer empId){
        boolean b = employeeService.removeById(empId);

        if (!b){
            ResultJson.buildError("删除不成功");
        }

        return ResultJson.buildSuccess();
    }
}

